<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="位置" prop="node">
        <el-select v-model="queryParams.node" placeholder="请选择" @change="getListType">
          <el-option
            v-for="item in optionsArea"
            :key="item.id"
            :label="item.areaName"
            :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentType">
        <el-select v-model="queryParams.equipmentType" placeholder="请选择" @change="getListGroup">
          <el-option
            v-for="item in optionsType"
            :key="item.id"
            :label="item.typeName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="dept">
        <el-select v-model="queryParams.dept" placeholder="请选择" @change="getListEquipment">
          <el-option
            v-for="item in optionsGroup"
            :key="item.id"
            :label="item.equipmentGroupName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="equipment">
        <el-select v-model="queryParams.id" placeholder="请选择" >
          <el-option
            v-for="item in equipmentListS"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="startDate">
        <el-date-picker
          clearable
          v-model="queryParams.startDate"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="endDate">
        <el-date-picker
          clearable
          v-model="queryParams.endDate"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iot:equipment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="位置" align="center" prop="areaName" />
      <el-table-column label="设备类型" align="center" prop="typeName" />
      <el-table-column label="设备组" align="center" prop="groupName" />
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="数值" align="center" prop="value" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="时间" align="center" prop="time" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listEquipmentAll,histroyListPage } from "@/api/iot/equipment";
import { listTypeByArea} from "@/api/iot/type";
import { listAreaAll} from "@/api/iot/area";
import { listGroupAll} from "@/api/iot/group";

export default {
  name: "Equipment",
  data() {
    return {
      // 遮罩层
      loading: true,
      optionsArea:[],
      optionsType:[],
      optionsGroup:[],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      equipmentList: [],
      equipmentListS: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id:null,
        name: null,
        equipmentType: null,
        describe: null,
        dept: null,
        address: null,
        gateway: null,
        status: null,
        node: null,
        analog: null,
        offset: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getListArea();
  },
  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      histroyListPage(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询区域列表 */
    getListArea() {
      this.loading = true;
      listAreaAll(this.queryParams).then(response => {
        this.optionsArea = response.rows;
        this.loading = false;

      });
    },
    /** 查询区域列表 */
    getListType(areaId) {
      this.areaId=areaId;
      this.optionsType =[];
      this.loading = true;
      listTypeByArea(areaId).then(response => {
        this.optionsType = response.rows;
        this.loading = false;

      });
    },
    /** 查询区域列表 */
    getListGroup(typeId) {
      this.loading = true;
      this.optionsGroup=[];

      listGroupAll(typeId,this.areaId).then(response => {
        this.optionsGroup = response.rows;
        this.loading = false;

      });
    },
    /** 查询区域列表 */
    getListEquipment() {
      this.loading = true;
      this.equipmentListS=[];
      let queryParam= { dept: this.queryParams.dept}
      listEquipmentAll(queryParam).then(response => {
        this.equipmentListS = response.rows;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        equipmentType: null,
        describe: null,
        dept: null,
        address: null,
        gateway: null,
        status: 0,
        node: null,
        analog: null,
        offset: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('iot/equipment/export', {
        ...this.queryParams
      }, `equipment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
